package com.osmap.openstreetmap.adapter;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.osmap.openstreetmap.R;
import com.osmap.openstreetmap.bean.ShareBeans;
import com.osmap.openstreetmap.ui.activity.ViewRealtimeOSM;
import com.osmap.openstreetmap.utils.ScreenProperties;

/**
 * Created with IntelliJ IDEA. User: ys.peng Date: 13-12-27 Time: 下午2:16 To
 * change this template use File | Settings | File Templates.
 */
public class NewsPager extends BaseAdapter {
	private Context context;
	public List<ShareBeans> list;
	public LayoutInflater mInflater;

	// 线程池管理轮播图线程 自动切换
	private Executor executor = Executors.newFixedThreadPool(10);
	// 记录轮播图当前位置
	private int currentPosition = 0;

	private AdHolder adHolder = null;

	public NewsPager(Context context, List<ShareBeans> list) {
		this.context = context;
		this.list = list;
		mInflater = LayoutInflater.from(context);
	}

	@Override
	public int getCount() {
		if (list != null) {
			return list.size() + 1; // To change body of implemented methods use
			// File | Settings | File Templates.
		} else {
			return 0;
		}
	}

	@Override
	public Object getItem(int i) {
		return null; // To change body of implemented methods use File |
		// Settings | File Templates.
	}

	@Override
	public long getItemId(int i) {
		return 0; // To change body of implemented methods use File | Settings |
		// File Templates.
	}

	View adView;

	@Override
	public View getView(int position, View convertView, ViewGroup viewGroup) {
		if (position == 0) {
			// 轮播图 设置
			if (adView == null) {
				adHolder = new AdHolder();
				adView = mInflater.inflate(R.layout.item_advertise, null);
				initViewPagerBound(adView);
				adHolder.viewPager = (ViewPager) adView
						.findViewById(R.id.item_vp);
				adHolder.radioGroup = (RadioGroup) adView
						.findViewById(R.id.rg_points);
				adHolder.tv = (TextView) adView
						.findViewById(R.id.tv_video_brief);
				if (list != null) {
					fillPager(adHolder, list);
				}
				return adView;
			} else {
				return adView;
			}
		} else {
			ViewHolder holder = null;
			if (convertView == null) {
				holder = new ViewHolder();
				convertView = mInflater.inflate(R.layout.item_map, null);
				holder.title = (TextView) convertView
						.findViewById(R.id.tv_title);
				holder.time = (TextView) convertView
						.findViewById(R.id.tv_publishtime);
				holder.collectCount = (TextView) convertView
						.findViewById(R.id.tv_collect_time);
				holder.desc = (TextView) convertView.findViewById(R.id.tv_desc);
				holder.mapImg = (ImageView) convertView
						.findViewById(R.id.iv_map_min);
				holder.headImg = (ImageView) convertView
						.findViewById(R.id.iv_headimg);
				holder.mapCount = (TextView) convertView
						.findViewById(R.id.tv_footmark_miles);
				convertView.setTag(holder);
			}
			ShareBeans beans = list.get(position - 1);
			if (holder == null) {
				holder = new ViewHolder();
				convertView = mInflater.inflate(R.layout.item_map, null);
				holder.title = (TextView) convertView
						.findViewById(R.id.tv_title);
				holder.time = (TextView) convertView
						.findViewById(R.id.tv_publishtime);
				holder.collectCount = (TextView) convertView
						.findViewById(R.id.tv_collect_time);
				holder.desc = (TextView) convertView.findViewById(R.id.tv_desc);
				holder.mapImg = (ImageView) convertView
						.findViewById(R.id.iv_map_min);
				holder.headImg = (ImageView) convertView
						.findViewById(R.id.iv_headimg);
				holder.mapCount = (TextView) convertView
						.findViewById(R.id.tv_footmark_miles);
				convertView.setTag(holder);
			}
			holder = (ViewHolder) convertView.getTag();
			holder.title.setText(beans.nickname);
			holder.time.setText(beans.publishTime + "分钟前发布");
			holder.mapCount.setText(beans.miles + "公里");
			holder.collectCount.setText(beans.collectionCount + "次收藏");
			holder.desc.setText(beans.desc);
			convertView.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					Intent intent = new Intent(context, ViewRealtimeOSM.class);
					context.startActivity(intent);
				}
			});
			return convertView;
		}
	}

	private void initViewPagerBound(View view) {
		// 计算图片宽度在屏幕中占多少英寸
		float xInch = (ScreenProperties.getScreenWidth() / ScreenProperties
				.getXdpi());
		// 根据图片宽高比例算出高应该占多少英寸
		double yInch = xInch * (15 / 28.0);
		// 再根据Y轴方向上每英寸多少像素算出图片高应该有多少像素
		int viewPagerHeight = (int) (ScreenProperties.getXdpi() * yInch);
		ViewPager viewPagerBound = (ViewPager) view.findViewById(R.id.item_vp);
		viewPagerBound.setLayoutParams(new RelativeLayout.LayoutParams(
				LayoutParams.MATCH_PARENT, viewPagerHeight));
	}

	private void initListener(final AdHolder adHolder) {
		// 线程池开启 轮播线程
		executor.execute(new Runnable() {
			@Override
			public void run() {
				while (true) {
					try {
						Thread.sleep(3000);
					} catch (InterruptedException e) {
						e.printStackTrace(); // To change body of catch
						// statement use File | Settings
						// | File Templates.
					}
					currentPosition = currentPosition + 1;
					Message message = Message.obtain();
					message.obj = adHolder.viewPager;
					handler.sendMessage(message);
				}
			}
		});

		adHolder.viewPager
		.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
			@Override
			public void onPageScrolled(int i, float v, int i2) {
				// To change body of implemented methods use File |
				// Settings | File Templates.
			}

			@Override
			public void onPageSelected(int i) {
				// renderBrief(i, adHolder);
				adjustPoint(i, adHolder);
				currentPosition = i;
			}

			@Override
			public void onPageScrollStateChanged(int i) {
				// To change body of implemented methods use File |
				// Settings | File Templates.
			}
		});

	}

	// 处理轮播图发送的消息
	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			if (currentPosition > list.size()) {
				currentPosition = 0;
			}
			try {
				ViewPager viewPager = (ViewPager) msg.obj;
				viewPager.setCurrentItem(currentPosition);

			} catch (Exception e) {

			}
		}
	};

	// 填充pager里面的数据
	private void fillPager(AdHolder adHolder, List<ShareBeans> wheel) {
		List<View> list = new ArrayList<View>();

		// for (int i = 0; i < wheel.size(); i++) {
		// View view = mInflater.inflate(R.layout.item_firstloading, null);
		//
		// list.add(view);
		// }

		View view = mInflater.inflate(R.layout.item_firstloading, null);
		list.add(view);

		try {
			// 防止里面的数据为null
			initListener(adHolder);
			// addPointView(newsListJson.getData().getWheel().size(), adHolder);
			NewsAdAdapter firstLoadingPager = new NewsAdAdapter(list, context);
			adHolder.viewPager.setAdapter(firstLoadingPager);
		} catch (Exception e) {

		}
	}

	// private void fillData(ViewHolder viewHolder, NewsBean newsJson) {
	// if (newsJson.getNewsType() == 1) {
	// viewHolder.newsType.setVisibility(View.VISIBLE);
	// } else {
	// viewHolder.newsType.setVisibility(View.INVISIBLE);
	// }
	// viewHolder.newsShortDesc.setText(newsJson.getNewsSummary());
	// viewHolder.newsTitle.setText(newsJson.getMainTitle());
	// if (newsJson.getPreviewPicUr() != null) {
	// aQuery.id(viewHolder.newsPic).image(ServerUrl.DOMAIN +
	// newsJson.getPreviewPicUr(), true, true);
	// }
	// }

	static class ViewHolder {
		public TextView title;
		public TextView time;
		public TextView desc;
		public TextView collectCount;
		public TextView mapCount;
		public TextView milesCount;
		public ImageView headImg;
		public ImageView mapImg;
	}

	// 动态添加轮播图底下的原点
	private void addPointView(int count, AdHolder adHolder) {
		if (count > 0) {
			for (int i = 0; i < count; i++) {
				// oom溢出处理
				RadioButton radBtn = new RadioButton(context);
				radBtn.setClickable(false);
				int width = (int) ScreenProperties.getScreenDensity() * 12;
				RadioGroup.LayoutParams params = new RadioGroup.LayoutParams(
						width, LayoutParams.WRAP_CONTENT);
				// radBtn.setButtonDrawable(R.drawable.news_wheel);
				adHolder.radioGroup.addView(radBtn, params);
			}
		}

		View v = adHolder.radioGroup.getChildAt(0);
		RadioButton radioBtn = (RadioButton) v;
		radioBtn.setChecked(true);
	}

	// 原点变化
	private void adjustPoint(int pageIndex, AdHolder adHolder) {
		View child = adHolder.radioGroup.getChildAt(pageIndex);
		if (child instanceof RadioButton) {
			RadioButton radBtn = (RadioButton) child;
			radBtn.setChecked(true);
		}
	}

	class AdHolder {
		private ViewPager viewPager;
		private TextView tv;
		private RadioGroup radioGroup;
	}
}
